//
//  NewsViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsCell.h"
#import "NewsModel.h"

@interface NewsViewController () {
    
    NSMutableArray *_dataArray;
    UIImageView *_headerImageView;
    UIView *_backgroundViewOfView;
}



@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadData];
    [self _setUI];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_loadData{
    _dataArray = [NSMutableArray array];
    
    //NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"news_list" ofType:@"json"] options:NSJSONReadingMutableContainers error:nil]];
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"news_list" ofType:@"json"]];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary *dic in array) {
        NewsModel *model = [[NewsModel alloc] init];
        model.title = dic[@"title"];
        model.summary = dic[@"summary"];
        model.image = dic[@"image"];
        model.newsId = [dic[@"newsid"] integerValue];
        model.newsType = (NewsType)[dic[@"type"] integerValue];
        
        [_dataArray addObject:model];    }
    
}

- (void)_setUI{
    
    _tableVew.backgroundColor = [UIColor clearColor];
    _tableVew.separatorColor = kTableViewSeColor;
    _tableVew.rowHeight = 100;
    
    NewsModel *model = _dataArray[0];
    _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 150)];
    [_headerImageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
    [self.view insertSubview:_headerImageView belowSubview:_tableVew];
    
    
    _backgroundViewOfView = [[UIView alloc] init];
    _backgroundViewOfView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [_headerImageView addSubview:_backgroundViewOfView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, 36)];
    [_backgroundViewOfView addSubview:label];
    label.text = model.title;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:17];
    label.textAlignment = NSTextAlignmentCenter;
    CGFloat y = CGRectGetMaxY(_headerImageView.bounds);
    _backgroundViewOfView.frame = CGRectMake(0, y - 28, kScreenWidth, 32);
    
    
    [self.tableVew registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:@"NewsCell"];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 150)];
    view.backgroundColor = [UIColor clearColor];
    self.tableVew.tableHeaderView = view;
    [_dataArray removeObjectAtIndex:0];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat ty = _tableVew.contentOffset.y;
    CGFloat top = _tableVew.contentInset.top;
    CGFloat y = ty + top;
    if (y > 0 ) {
        _headerImageView.frame = CGRectMake(0, 64 - y, kScreenWidth, 150);
    }else {
        CGFloat width = kScreenWidth * (150 - y) / 150;
        CGFloat height = 150 - y;
        _headerImageView.frame = CGRectMake((kScreenWidth - width) / 2, 64, width, height);
        CGFloat y1 = CGRectGetMaxY(_headerImageView.bounds);
        _backgroundViewOfView.frame = CGRectMake(- (kScreenWidth - width) / 2, y1 - 32, kScreenWidth, 32);
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsCell *cell = [_tableVew dequeueReusableCellWithIdentifier:@"NewsCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];

    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     NewsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *viewControllerId = [NSString string];
    switch (cell.model.newsType) {
        case WordType:
            viewControllerId = @"NewsWordViewController";
            break;
        case ImageType:
            viewControllerId = @"NewsImageViewController";
            break;
        case VideoType:
            viewControllerId = @"NewsVideoViewController";
            break;
        default:
            break;
    }
    UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:viewControllerId];
    viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
